package Demo1;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * Created with  IntelliJ IDEA .
 * Description:
 * User: lenovo
 * Date: 2025-${ MONTH }-21
 * Time: 19:53
 */
class Student implements Comparable<Student>{

    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public int compareTo(Student o){
        if(this.age > o.age){
            return 1;
        } else if (this.age == o.age) {
            return 0;
        }else{
            return -1;
        }
        /* return this.name.compareTo(o.name);*/
    }

}


public class Test {
    public static void mySort(Comparable[] comparables){

        for (int i = 0; i < comparables.length-1; i++) {

            for (int j = 0; j < comparables.length-1-i; j++) {

                if(comparables[j].compareTo(comparables[j+1])>0){
                    Comparable temp =comparables[j];
                    comparables[j] =comparables[j+1];
                    comparables[j+1] = temp;
                }
            }
        }
    }
    public static void main(String[] args) {


        Student[] students =new Student[3];

        students[0] = new Student("zhangsan",55);
        students[1] = new Student("lisi",15);
        students[2] = new Student("wangwu",8);

        System.out.println("排序前："+ Arrays.toString(students));

        System.out.println("=======");

       /* Arrays.sort(students); */
        mySort(students);

        System.out.println("排序后："+Arrays.toString(students));

       /* System.out.println(students[1].compareTo(students[2]));*/

    }
}
